﻿using Newtonsoft.Json;
using NP.WebAPI.Models.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApplication1
{
    //回收库关键词数据管理
    class RecycleKeywordsDataManager
    {
        public delegate void SuccessedHandle(List<RecycleResInfoModel> list);
        public delegate void FailedHandle(WebException exception);

        public ReqGetRecycleKeywordModel requestModel { set; get; }

        public int totalCount;
        public int totalPage;
        //获取得到的数据源
        public List<RecycleResInfoModel> dataList { set; get; }

        /// <summary>
        /// 无参构造函数
        /// </summary>
        public RecycleKeywordsDataManager()
        {
            this.requestModel = new ReqGetRecycleKeywordModel("A", 2, "", 10, 1);
            dataList = new List<RecycleResInfoModel>();
        }

        /// <summary>
        /// 获取首页数据
        /// </summary>
        /// <param name="successedFunc"></param>
        /// <param name="failedFunc"></param>
        public void getFirstPageData(SuccessedHandle successedFunc, FailedHandle failedFunc)
        {
            getPageDataWithPageIndex(1, successedFunc, failedFunc);
        }

        /// <summary>
        /// 获取下一页数据
        /// </summary>
        /// <param name="successedFunc"></param>
        /// <param name="failedFunc"></param>
        public void getNextPageData(SuccessedHandle successedFunc, FailedHandle failedFunc)
        {
            getPageDataWithPageIndex(requestModel.pageIndex + 1, successedFunc, failedFunc);
        }

        /// <summary>
        /// 获取最后一页数据
        /// </summary>
        /// <param name="successedFunc"></param>
        /// <param name="failedFunc"></param>
        public void getLastPageData(SuccessedHandle successedFunc, FailedHandle failedFunc)
        {
            getPageDataWithPageIndex(totalPage, successedFunc, failedFunc);
        }

        /// <summary>
        /// 获取上一页数据
        /// </summary>
        /// <param name="successedFunc"></param>
        /// <param name="failedFunc"></param>
        public void getPreviousPageData(SuccessedHandle successedFunc, FailedHandle failedFunc)
        {
            getPageDataWithPageIndex(requestModel.pageIndex - 1, successedFunc, failedFunc);
        }

        /// <summary>
        /// 获取某一页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="successedFunc"></param>
        /// <param name="failedFunc"></param>
        public void getPageDataWithPageIndex(int pageIndex,SuccessedHandle successedFunc, FailedHandle failedFunc)
        {
            List<RecycleResInfoModel> recycleList = new List<RecycleResInfoModel>();

            requestModel.pageIndex = pageIndex;

            Redslide.HttpLib.Request.Post("http://172.16.51.35:8086/api/KeywordRecycle/SearchRecycleKeywordList",
                requestModel,
                result =>
                {
                    BaseResponse<ResGetRecycleKeywordModel> responseModel = JsonConvert.DeserializeObject<BaseResponse<ResGetRecycleKeywordModel>>(result);

                    if (responseModel.ErrorCode == -1)
                    {
                        foreach (RecycleKeywordModel keywordModel in responseModel.Data.RecycleKeywordList)
                        {
                            recycleList.Add(new RecycleResInfoModel((pageIndex - 1) * 10 + responseModel.Data.RecycleKeywordList.IndexOf(keywordModel) + 1, 
                                keywordModel.XH,
                                keywordModel.KeywordName,
                                keywordModel.Frequency,
                                keywordModel.StudyName, 
                                keywordModel.SourceFlag == 0 ? "互联网" : "教学资源"));
                        }

                        totalCount = responseModel.TotalCount;

                        totalPage = (int)Math.Ceiling(((float)totalCount) / requestModel.pageSize);
                        successedFunc(recycleList);
                    }
                    else
                    {
                        failedFunc(new WebException(responseModel.Msg));
                    }
                }, exception => {
                    failedFunc(exception);
                });
        }

        public void removeRecycleKeyword(List<int> list, Action successCallback, Action<WebException> failCallback)
        {
            Redslide.HttpLib.Request.Post("http://172.16.51.35:8086/api/KeywordRecycle/RemoveRecycleKeyword ",
                new { xhList = list, subjectId = "A"},
                result =>
                {
                    BaseResponse<ResGetRecycleKeywordModel> responseModel = JsonConvert.DeserializeObject<BaseResponse<ResGetRecycleKeywordModel>>(result);

                    if (responseModel.ErrorCode == -1)
                    {
                        successCallback();
                    }
                    else
                    {
                        failCallback(new WebException(responseModel.Msg));
                    }

                }, exception => {
                    failCallback(exception);
                });
        }
    }
}
